//
//  MYDynamicDetailsGoodsCell.m
//  MuYing
//
//  Created by tbb on 2019/7/24.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYDynamicDetailsGoodsCell.h"
#import "MYDynamicDetailsGoodsCollCell.h"
@interface MYDynamicDetailsGoodsCell ()<UICollectionViewDelegate, UICollectionViewDataSource>


@end

@implementation MYDynamicDetailsGoodsCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.goodsCollView];
        [self.goodsCollView mas_makeConstraints:^(MASConstraintMaker *make) {
            //        make.edges.mas_equalTo(0);
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.left.mas_equalTo(0);
        }];
    }
    return self;
}

#pragma makr -- lazy
-(UICollectionView *)goodsCollView {
    if (!_goodsCollView) {
        self.goodsLayout = [[UICollectionViewFlowLayout alloc]init];
        self.goodsLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.goodsLayout.minimumLineSpacing = 10;
        self.goodsLayout.minimumInteritemSpacing = 10;
//        edgeInsets
        self.goodsLayout.sectionInset = UIEdgeInsetsMake(12, 12, 18, 12);
        _goodsCollView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.goodsLayout];
         _goodsCollView.showsVerticalScrollIndicator = NO;
        _goodsCollView.backgroundColor = UIColor.whiteColor;
        _goodsCollView.dataSource = self;
        _goodsCollView.delegate = self;
        _goodsCollView.bounces = NO;
        _goodsCollView.scrollEnabled = NO;
        [_goodsCollView registerNib:[UINib nibWithNibName:@"MYDynamicDetailsGoodsCollCell" bundle:nil] forCellWithReuseIdentifier:@"MYDynamicDetailsGoodsCollCell"];
        
    }
    return _goodsCollView;
}

#pragma makr -- lazy end

#pragma mark --UICollectionViewDelegate, UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.goodArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YQPSkuModel *model = self.goodArray[indexPath.row];
    MYDynamicDetailsGoodsCollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MYDynamicDetailsGoodsCollCell" forIndexPath:indexPath];
    [cell.goodsImageView sd_setImageWithURL:[NSURL URLWithString:model.defaultImage] placeholderImage:IMAGE_NAMED(@"goods_test_icon")];
    if ([model.skuName removeStringSpace].length > 0 ) {
        cell.titleLb.text = model.skuName;
    }else{
        cell.titleLb.text = model.productName;
    }
    cell.priceLb.text = [NSString stringWithFormat:@"￥%.2f",model.price.doubleValue];
    cell.salesVolumeLb.text = [NSString stringWithFormat:@"%zd%@",model.payNumber,kYQLS(@"已购")];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat w = (kScreenWidth - 34) / 2.0;
    return CGSizeMake(w, w + 70);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(didSelectItemAtItem:)]) {
        [self.delegate didSelectItemAtItem:indexPath.item];
    }
}

#pragma mark --UICollectionViewDelegate, UICollectionViewDataSource end
-(void)layoutSubviews {
    [super layoutSubviews];
}

-(void)setGoodArray:(NSArray *)goodArray {
    _goodArray = [goodArray copy];
    CGFloat w = (kScreenWidth - 34) / 2.0;
    NSInteger row = self.goodArray.count/2;
    if (self.goodArray.count % 2 != 0) {
        row += 1;
    }

    CGFloat collH = row * (w + 70) + (row-1)*10+30;//[self.goodsLayout collectionViewContentSize].height;
    
    if (self.collViewH != collH) {
        if ([self.delegate respondsToSelector:@selector(updateCollViewH:)]) {
            [self.delegate updateCollViewH:collH];
        }
    }
}

@end

